﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

public partial class ManterFormato : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBoxNome.Enabled = true;
        DropDownListTipo.Enabled = true;
        ButtonCancelar.Enabled = true;
        ButtonGravar.Enabled = true;
        ButtonNovoFormato.Enabled = false;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ButtonGravar_Click(object sender, EventArgs e)
    {
        try
        {
            FormatoBLL fBLL = new FormatoBLL();
            if (TextBoxCodigo.Text == "")
            {
                Formato f = new Formato(TextBoxNome.Text, Convert.ToInt32(DropDownListTipo.SelectedValue));
                fBLL.Insere(f);
            }
            else
            {
                Formato f = new Formato(Convert.ToInt32(TextBoxCodigo.Text), TextBoxNome.Text, Convert.ToInt32(DropDownListTipo.SelectedValue));
                fBLL.Atualiza(f);

            }
            cancelar();
            GridView1.DataBind();
            ButtonNovoFormato.Enabled = true;
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
    private void cancelar()
    {
        TextBoxNome.Text = "";
        TextBoxCodigo.Text = "";
        DropDownListTipo.SelectedValue= "0";
        TextBoxNome.Enabled = false;
        DropDownListTipo.Enabled = false;
        ButtonCancelar.Enabled = false;
        ButtonGravar.Enabled = false;
        ButtonNovoFormato.Enabled = true;
        lbErro.Text = "";
    }
    protected void ButtonCancelar_Click(object sender, EventArgs e)
    {
        cancelar();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            FormatoBLL formato = new FormatoBLL();
            Formato f = new Formato(id);
            formato.Remove(f);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            FormatoBLL formato = new FormatoBLL();
            Formato f = formato.Consulta(id);

            TextBoxCodigo.Text = f.Codigo.ToString();
            TextBoxNome.Text = f.Nome;
            DropDownListTipo.SelectedValue = f.Tipo.ToString();

            TextBoxNome.Enabled = true;
            DropDownListTipo.Enabled = true;
            ButtonCancelar.Enabled = true;
            ButtonGravar.Enabled = true;
            ButtonNovoFormato.Enabled = false;
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

